/****************************************************************************
@FILENAME:	codec_433.c
@FUNCTION:	
@AUTHOR:	flege.zhang
@DATE:		2020.04.12
*****************************************************************************/


#include "codec_433.h"
#include "hal_gpio.h"
#include "key_process.h"



#if(HW_DETECT_TYPE == DECODE_433_TYPE)
uint8_t decoded_curr_state = DEC_433_HEADER;
uint8_t decoded_curr_bit = 32;
uint16_t last_time_high = 0x0;


extern str_key_info key_info;

void decode_433_init()
{
	decoded_curr_state = DEC_433_HEADER;
	decoded_curr_bit = 32;

	memset(&key_info,0x0,sizeof(str_key_info));
}

void decode_433(uint16_t time_low,uint16_t time_high)
{
	switch(decoded_curr_state)
	{
		case DEC_433_HEADER:
		{
			key_info.curr_key = 0x0;
			if(IS_433_HEAD(last_time_high,time_low))
			{
				decoded_curr_state = DEC_433_DATA;
				decoded_curr_bit = 32;
			}
			break;
		}
		case DEC_433_DATA:
		{
			--decoded_curr_bit;

			if(IS_433_BIT1(last_time_high,time_low))
			{
				key_info.curr_key |= 1<<decoded_curr_bit;
			}

			//check decode end?
			if(0 == decoded_curr_bit)
			{
				key_info.handle_flag = 0x01;
				
				if(key_info.last_key == key_info.curr_key)
				{
					++key_info.pressed_time;
				}
				else
				{
					key_info.pressed_time = 0x0;
				}

				key_info.last_key = key_info.curr_key;
				
				decoded_curr_state = DEC_433_HEADER;
#if LOG_SECONDARY
				print("D:%x,C:%d\n",key_info.curr_key,key_info.pressed_time);
#endif
			}
			break;
		}
		default:
		{
			break;
		}
	}

	//must to do!!!
	last_time_high = time_high;
}

void decode_433_end()
{
	decode_433_init();
	
#if LOG_SECONDARY
	print("T\n");
#endif
}
#endif
